package com.nus;

import java.io.BufferedReader;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.StringTokenizer;

import com.nus.parsers.ParserAPIs;
import com.nus.representations.IvleNews;
import com.nus.representations.ShuttleMapLocations;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.Uri;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;

public class Shuttle_Map_Activity<Address> extends Activity {
	public ImageButton home;
	public static ArrayList<ShuttleMapLocations> smap;
	public static String title;
	public static String start_latitude;
	public static String start_longitude;
	public static String end_latitude;
	public static String end_longitude;
	public CharSequence cs4;
	public CharSequence cs5;
	public CharSequence cs6;
	private String lv_arr[];
	private double latitude;
	private double longitude;
	public String area;
	 public StringBuilder total = new StringBuilder();
	 protected TextView titletheme;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.shuttlelayout);
		 getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
		 titletheme = (TextView)findViewById(R.id.titleheader);
		 titletheme.setText("NUS Navigator");
		 ImageView b = (ImageView)findViewById(R.id.home);
		  b.setImageDrawable(getResources().getDrawable(R.drawable.maps));
		try {
			 // open the file for reading
		    InputStream instream = getResources().openRawResource(
					R.raw.building);
		 
		    // if file the available for reading
		    if (instream != null) {
		      // prepare the file for reading
		      InputStreamReader inputreader = new InputStreamReader(instream);
		      BufferedReader buffreader = new BufferedReader(inputreader);
		     

		      String line;
		 
		      // read every line of the file into the line-variable, on line at the time
		      while (( line = buffreader.readLine()) != null) {
		        // do something with the settings from the file
		    	  total.append(line);
		      }
		      instream.close();
		    }
			String response = total.toString();
			
			Log.i("ResponseRohit", response);
			smap = ParserAPIs.parseShuttleMapLocations(response);
			Log.i("rohitSize", smap.size() + "");
			lv_arr = new String[smap.size()];
			for (int i = 0; i<smap.size(); i++) {
				lv_arr[i] = smap.get(i).getTitle();
				Log.i("rohitTitle", smap.get(i).getTitle());
			}

			
			LocationManager locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
			LocationListener locListener = new MyLocationListener();
			locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locListener);
			Location location = locManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
			if(location != null)
			{
			  latitude = location.getLatitude();
			  longitude = location.getLongitude();		 
			}

				
			final Spinner spinner = (Spinner) findViewById(R.id.start_menu);

			// Application of the Array to the Spinner
			final ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
					this, android.R.layout.simple_spinner_item, lv_arr);
			spinnerArrayAdapter
					.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
			spinner.setAdapter(spinnerArrayAdapter);
			spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

				
				public void onItemSelected(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					// TODO Auto-generated method stub
					int id = (int) spinner.getItemIdAtPosition(arg2);
					cs4 = spinnerArrayAdapter.getItem(arg2);
					if(!smap.get(id).getTitle().contains("My Location")){
						
						start_latitude = smap.get(id).getLatitude();
						start_longitude = smap.get(id).getLongitude();
						Log.i("select court", cs4.toString());
						Log.i("lat", start_latitude);
						
					}
					else{
						
						start_latitude = new Double(latitude).toString();
						start_longitude = new Double(longitude).toString();
						
						Geocoder geo = new Geocoder(getApplicationContext(), Locale.getDefault());
						List<android.location.Address> addresses;
						try {
							addresses = geo.getFromLocation(latitude, longitude, 1);
							if (addresses.size() > 0) {
								area = addresses.get(0).getLocality();
								//Log.i("area", area);
								//Toast.makeText( getApplicationContext(),"My location is" +area, Toast.LENGTH_SHORT ).show();
							}
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						//Log.i("select court", cs4.toString());
						Log.i("startlat", start_latitude);
					}
				}

				@Override
				public void onNothingSelected(AdapterView<?> arg0) {
					// TODO Auto-generated method stub

				}
			});

			final Spinner spinner1 = (Spinner) findViewById(R.id.end_spinner);

			// Application of the Array to the Spinner
			final ArrayAdapter<String> spinnerArrayAdapter1 = new ArrayAdapter<String>(
					this, android.R.layout.simple_spinner_item, lv_arr);
			spinnerArrayAdapter1
					.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
			spinner1.setAdapter(spinnerArrayAdapter1);
			spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

				@Override
				public void onItemSelected(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					// TODO Auto-generated method stub
					int id = (int) spinner1.getItemIdAtPosition(arg2);
					cs5 = spinnerArrayAdapter1.getItem(arg2);
					if(!smap.get(id).getTitle().contains("My Location")){
						
						end_latitude = smap.get(id).getLatitude();
						end_longitude = smap.get(id).getLongitude();
					
					}
					else{
						
						end_latitude = new Double(latitude).toString();
						end_longitude = new Double(longitude).toString();
						
						Geocoder geo = new Geocoder(getApplicationContext(), Locale.getDefault());
						List<android.location.Address> addresses;
						try {
							addresses = geo.getFromLocation(latitude, longitude, 1);
							if (addresses.size() > 0) {
								area = addresses.get(0).getLocality();
							//	Log.i("area", area);
							//	Toast.makeText( getApplicationContext(),"My location is" +area, Toast.LENGTH_SHORT ).show();
							}
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						//Log.i("select court", cs4.toString());
						Log.i("endlat", end_latitude);
					}
				}

				@Override
				public void onNothingSelected(AdapterView<?> arg0) {
					// TODO Auto-generated method stub

				}
			});

			final Button gomap = (Button) findViewById(R.id.go_button);
			gomap.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					// startActivity(new Intent("com.nus.ShuttleMaps"));
						
						//check = 2;
						String uri = String
						.format("http://maps.google.com/maps?saddr="
								+ start_latitude + "," + start_longitude
								+ "&daddr=" + end_latitude + ","
								+ end_longitude);
				 Intent intent = new Intent(Intent.ACTION_VIEW, Uri
						.parse(uri));
				 startActivity(intent);
				
				}
			});
			
			
			final Spinner spinner11 = (Spinner) findViewById(R.id.spin_gothere);

			// Application of the Array to the Spinner
			final ArrayAdapter<String> spinnerArrayAdapter11 = new ArrayAdapter<String>(
					this, android.R.layout.simple_spinner_item, lv_arr);
			spinnerArrayAdapter11
					.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
			spinner11.setAdapter(spinnerArrayAdapter11);
			spinner11.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

				@Override
				public void onItemSelected(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					// TODO Auto-generated method stub
					int id = (int) spinner11.getItemIdAtPosition(arg2);
					cs6 = spinnerArrayAdapter11.getItem(arg2);
					if(!smap.get(id).getTitle().contains("My Location")){
						
						end_latitude = smap.get(id).getLatitude();
						end_longitude = smap.get(id).getLongitude();
					
					}
					else{
						
						end_latitude = new Double(latitude).toString();
						end_longitude = new Double(longitude).toString();
						
//						Geocoder geo = new Geocoder(getApplicationContext(), Locale.getDefault());
//						List<android.location.Address> addresses;
//						try {
//							addresses = geo.getFromLocation(latitude, longitude, 1);
//							if (addresses.size() > 0) {
//								area = addresses.get(0).getLocality();
//							//	Log.i("area", area);
//							//	Toast.makeText( getApplicationContext(),"My location is" +area, Toast.LENGTH_SHORT ).show();
//							}
//						} catch (IOException e) {
//							// TODO Auto-generated catch block
//							e.printStackTrace();
//						}
//						//Log.i("select court", cs4.toString());
//						Log.i("endlat", end_latitude);
					}
				}

				@Override
				public void onNothingSelected(AdapterView<?> arg0) {
					// TODO Auto-generated method stub

				}
			});
			
			Button gothere = (Button) findViewById(R.id.btn_gother);
			gothere.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent in = new Intent("com.nus.FindMeInMap");
					in.putExtra("lat",end_latitude );
					in.putExtra("long", end_longitude);
					startActivity(in);
				}
			});

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
	
}
